<template>
  <div class="link">
    <h3 class="title">友链管理</h3>
    <div class="toolbar">
      <el-button type="danger" size="small" icon="el-icon-plus" @click="showAddDialog = true">添加</el-button>
      <el-form inline @submit.native.prevent size="small">
        <el-form-item>
          <el-input v-model="searchForm.title" placeholder="友链名称" />
        </el-form-item>
        <el-button icon="el-icon-search" size="small" type="primary" @click="onSearch">搜索</el-button>
      </el-form>
    </div>
    <xu-table :data="tableData" :options="tableOptions" :pager="pager">
      <template slot="url" slot-scope="{ row }">
        <el-link underline icon="el-icon-link" :href="row.url">{{ row.url }}</el-link>
      </template>
      <template slot="status" slot-scope="{ row }">
        <el-switch active-value="1" inactive-value="0" v-model="row.status" @change="changeStatus(row)" />
      </template>
    </xu-table>

    <el-dialog :visible.sync="showAddDialog" :title="getTitle" width="500px">
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item label="友链名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入友链名称" />
        </el-form-item>
        <el-form-item label="友链地址" prop="url">
          <el-input prefix-icon="el-icon-link" v-model="form.url" placeholder="https://www.johnxu.net/" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input type="number" v-model="form.sort" style="width: 120px;" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <div>
            <el-switch v-model="form.status" active-value="1" inactive-value="0" />
          </div>
        </el-form-item>
      </el-form>
      <div class="footer" slot="footer">
        <el-button size="small" type="primary" @click="submit" :loading="loading">提交</el-button>
        <el-button size="small" @click="showAddDialog = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { LinkActionsEnum, LinkStatusEnum } from '../../../_/vue/store/link'
import table from '../../../mixins/table'
export default {
  mixins: [table],
  data () {
    return {
      loading: false,
      showAddDialog: false,
      tableOptions: [
        { attrs: { label: '排序', prop: 'sort', align: 'center', width: '80px' } },
        { attrs: { label: '友链名称', prop: 'title' } },
        { attrs: { label: '友链地址', prop: 'url' } },
        { attrs: { label: '是否启用', prop: 'status', align: 'center', width: '80px' } },
        {
          attrs: { label: '操作', prop: 'opt', width: '140px', align: 'center' },
          buttons: [
            { label: '编辑', icon: 'el-icon-edit', click: this.editRow },
            { label: '删除', icon: 'el-icon-delete', type: 'popover', click: this.deleteRow }
          ]
        }
      ],
      id: '',
      form: {
        title: '',
        url: '',
        sort: 0,
        status: LinkStatusEnum.ALLOW
      },
      rules: {
        title: [{ required: true, message: '友链名称不能为空', trigger: 'blur' }],
        url: [{ required: true, message: '友链地址不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    getTitle () {
      return this.id ? '编辑友链' : '添加友链'
    }
  },
  methods: {
    ...mapActions({
      list: LinkActionsEnum.LIST,
      edit: LinkActionsEnum.EDIT,
      save: LinkActionsEnum.SAVE,
      store: LinkActionsEnum.STORE,
      delete: LinkActionsEnum.DELETE
    }),
    async changeStatus (row) {
      try {
        await this.$store.dispatch(LinkActionsEnum.SWITCH, { id: row.id, status: row.status })
        this.$message.success('切换状态成功')
      } catch (err) {
        row.status = row.status === LinkStatusEnum.DENY ? LinkStatusEnum.ALLOW : LinkStatusEnum.DENY
      }
    },
    async editRow ({ row }) {
      this.id = row.id
      const resp = await this.edit(row.id)
      this.form = resp.data
      this.showAddDialog = true
    },
    async deleteRow ({ row }) {
      await this.delete({ ids: [row.id] })
      this.$message.success('删除成功')
      this.getData()
    },
    submit () {
      this.$refs.formRef.validate(async (valid) => {
        try {
          if (this.id) {
            await this.save({ id: this.id, data: this.form })
          } else {
            await this.store(this.form)
          }
          this.$message.success('操作成功')
          this.showAddDialog = false
          this.getData()
        } catch (err) {}
        this.loading = false
      })
    }
  }
}
</script>
